/**
 * Objeto RF_PREFS
 * Maneja las variables de la extensión
 *
 * @author Javier Pérez
 * @license GPL
 * @version 1.0
 */
var RF_PREFS = {
	pref: Components.classes["@mozilla.org/preferences-service;1"].
		getService(Components.interfaces.nsIPrefService).
		getBranch("rainfox."),
		
	observers: [],
		
	get: function(_name,_default) {
		switch (RF_PREFS.pref.getPrefType(_name)) {
			case RF_PREFS.pref.PREF_STRING:
				return RF_PREFS.pref.getCharPref(_name);
			case RF_PREFS.pref.PREF_BOOL:
				return RF_PREFS.pref.getBoolPref(_name);
			case RF_PREFS.pref.PREF_INT:
				return RF_PREFS.pref.getIntPref(_name);
		}
		return _default;
	},
	
	set: function(_name, _value) {
		switch (typeof(_value)) {
			case "string":
				RF_PREFS.pref.setCharPref(_name, _value);
				break;
			case "boolean":
				RF_PREFS.pref.setBoolPref(_name, _value);
				break;
			case "number":
				RF_PREFS.pref.setIntPref(_name, Math.floor(_value));
				break;
		}
	},

	/**
	 * whether a preference exists
	 */   
	exists: function(prefName) {
		return RF_PREFS.pref.getPrefType(prefName) != 0;
	},
	
	del: function(_name) {
		RF_PREFS.pref.deleteBranch(_name);
	},
	
	/**
	 * call a function whenever the named preference subtree changes
	 */
	watch: function(prefName, watcher) {
		// construct an observer
		var observer = {
			observe:function(subject, topic, prefName) {
				watcher(prefName);
			}
		};
	    // store the observer in case we need to remove it later
	    RF_PREFS.observers[watcher] = observer;
	
		RF_PREFS.pref.QueryInterface(Components.interfaces.nsIPrefBranchInternal)
			.addObserver(prefName, observer, false);
	},
  
	/**
	 * stop watching
	 */
	unwatch: function(prefName, watcher) {
		if (observers[watcher]) {
			RF_PREFS.pref.QueryInterface(Components.interfaces.nsIPrefBranchInternal)
				.removeObserver(prefName, observers[watcher]);
		}
	}
};

